<?php


namespace app\base\service;

use think\admin\service\AdminService;

use think\Cache;

/**
 * 火山大模型服务
 * Class AdminService
 * @package think\admin\service
 */
class HuoshanService extends AdminService
{

    protected $access_key = '';
    protected $appid = "";


    protected function getAuthorization($user_id)
    {
        $api_config_type = $this->app->db->name("ai_user")->where(['id'=>$user_id])->value("api_config_type");
        if($api_config_type==1){
            $user_id=10000;
        }
        $data = $this->app->db->name('ai_api_config')
            ->where(['user_id' => $user_id,'type'=>5])->find();
        if(empty($data)){
            return $this->resulet(0,'请先配置火山API参数');
        }else{
            $this->access_key = $data['access_key'];
            $this->appid = $data['appid'];
        }
    }

    protected function resulet($code=0,$msg='',$data=[])
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];
    }

    //文本生成
    public function createText($user_id,$system='',$user="")
    {
        $this->getAuthorization($user_id);
        $url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";

        $data = [
            "model"=>"deepseek-r1-250120",
            "messages"=>[
                [
                    "role"=>"system",
                    "content"=>$system
                ],
                [
                    "role"=>"user",
                    "content"=>$user
                ]
            ]
        ];

        $options['headers'] = ['Authorization:Bearer '.$this->access_key,'Content-Type: application/json'];
        $res = http_post($url,json_encode($data),$options);
        $res = json_decode($res,true);
        dump($res);
        if(isset($res['choices'][0]['message']['content'])){
            return  $this->resulet(1,'',$res['choices'][0]['message']['content']);

        }else{
            return  $this->resulet(0,'改写失败:'.$res['error']['message']);
        }
    }


    //音频生成
    public function createAudio($user_id,$content='')
    {
        $this->getAuthorization($user_id);
        $url = "https://openspeech.bytedance.com/api/v1/tts_async_with_emotion/submit";
        $data = [
            "appid"=>$this->appid,
            "text"=>$content,
            "voice_type"=>"BV123_streaming",
            "format"=>"mp3",
        ];
        $options['headers'] = ['Authorization:Bearer; '.$this->access_key,'Resource-Id: volc.tts_async.emotion'];
        $res = http_post($url,json_encode($data),$options);
        $res = json_decode($res,true);
        return $res;
    }

    //音频查询
    public function getAudioStatus($user_id,$task_id)
    {
        $this->getAuthorization($user_id);
        $url = "https://openspeech.bytedance.com/api/v1/tts_async_with_emotion/query";
        $data = [
            "appid"=>$this->appid,
            "task_id"=>$task_id,
        ];
        $options['headers'] = ['Authorization:Bearer; '.$this->access_key,'Resource-Id: volc.tts_async.emotion'];
        $res = http_get($url,$data,$options);
        $res = json_decode($res,true);
        return $res;
    }


}
